﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.Common.DictionaryCategory;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.Common.DictionaryCategory;
using BCCommon.Enums;

namespace BCTMSRESTful.Controllers.Management.General.DictionaryCategory
{
    /// <summary>
    /// 字典分类
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class DictionaryCategoryController : ManagementBaseController
    {
        private readonly IDictionaryCategoryService dictionaryCategoryService;

        public DictionaryCategoryController(IDictionaryCategoryService dictionaryCategoryService)
        {
            this.dictionaryCategoryService = dictionaryCategoryService;
        }

        #region 添加字典分类
        /// <summary>
        ///  添加字典分类
        /// </summary>
        /// <param name="requestDto">字典分类信息</param>
        /// <response code="201">添加字典分类成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加字典分类")]
        [ProducesResponseType(typeof(DictionaryCategoryDto), 201)]
        public IActionResult Post(
            [FromBody]DictionaryCategoryRequestDto requestDto)
        {
            var result = this.dictionaryCategoryService.Add(requestDto,this.Admin);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改字典分类
        /// <summary>
        ///  修改字典分类
        /// </summary>
        /// <param name="dictionaryCategoryId">字典分类Id</param>
        /// <param name="requestDto">字典分类信息</param>
        /// <response code="200">修改字典分类成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{dictionary_category_id}")]
        [Log("修改字典分类")]
        [ProducesResponseType(typeof(DictionaryCategoryDto), 200)]
        public IActionResult Put([FromRoute(Name = "dictionary_category_id")]long dictionaryCategoryId, [FromBody]DictionaryCategoryRequestDto requestDto)
        {
            DictionaryCategoryPutDto putDto = requestDto.As<DictionaryCategoryPutDto>();
            putDto.DictionaryCategoryId = dictionaryCategoryId;
            return StatusCode((int)HttpStatusCode.OK, this.dictionaryCategoryService.Update(putDto,this.Admin));
        }
        #endregion

        #region 删除字典分类
        /// <summary>
        ///  删除字典分类
        /// </summary>
        /// <param name="dictionaryCategoryId">字典分类Id</param>
        /// <response code="204">删除字典分类成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{dictionary_category_id}")]
        [Log("删除字典分类")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "dictionary_category_id")]long dictionaryCategoryId)
        {
            this.dictionaryCategoryService.Delete(dictionaryCategoryId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取字典分类
        /// <summary>
        ///  获取字典分类
        /// </summary>
        /// <param name="dictionaryCategoryId">字典分类Id</param>
        /// <response code="200">获取字典分类</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{dictionary_category_id}")]
        [ProducesResponseType(typeof(DictionaryCategoryDto), 200)]
        public IActionResult Get([FromRoute(Name = "dictionary_category_id")]long dictionaryCategoryId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.dictionaryCategoryService.Get(dictionaryCategoryId));
        }
        #endregion

        #region 获取字典分类列表
        /// <summary>
        ///  获取字典分类列表
        /// </summary>
        /// <param name="parentDictionaryCategoryId">父级字典分类Id</param>
        /// <param name="systemType">系统类型</param>
        /// <response code="200">获取字典分类列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<DictionaryCategoryDto>), 200)]
        public IActionResult GetList([FromQuery(Name ="parent_category_id")]long parentDictionaryCategoryId, [FromQuery(Name = "system_type")]BCSystemType? systemType)
        {
            if (systemType.HasValue)
            {
                return StatusCode((int)HttpStatusCode.OK, this.dictionaryCategoryService.GetList(systemType.Value));
            }
            else
            {
                return StatusCode((int)HttpStatusCode.OK, this.dictionaryCategoryService.GetList(parentDictionaryCategoryId));
            }
        }
        #endregion

        #region 获取字典分类（分页）
        /// <summary>
        ///  获取字典分类（分页）
        /// </summary>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取字典分类（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<DictionaryCategoryDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.dictionaryCategoryService.Query(start, end, sortName, ascending, index, size));
        }
        #endregion
    }
}

